package com.zqq.base.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zqq.base.R;
import com.zqq.base.utils.DpOrPxUtils;
import com.zqq.base.utils.LogUtils;
import com.zqq.loading.AVLoadingIndicatorView;


/**
 * description:默认: 按空白处不能取消动画 ;“返回键”不取消
 * project name: ProjectMaster
 * author: Drink1993
 * version 1.0
 * create at:  2017/8/15 11:34
 * change at:
 * change describe:
 * change version:
 */

public class PermissionDialog {

    private static volatile PermissionDialog permissionDialog;
    private TextView tv_dialog_title;
    private TextView tv_dialog_msg;
    private TextView tv_dialog_left;
    private TextView tv_dialog_right;
    private static volatile AlertDialog alertDialog;
    private Context context;

    public static PermissionDialog getInstance() {
        if (permissionDialog == null) {
            synchronized (PermissionDialog.class) {
                if (permissionDialog == null) {
                    permissionDialog = new PermissionDialog();
                }
            }
        }
        return permissionDialog;
    }


    public PermissionDialog createDialog(Context context) {
        LogUtils.i("bl_dialog", "context>>" + context);
        LogUtils.i("bl_dialog", "alertDialog>>" + alertDialog);
        if (alertDialog != null && alertDialog.isShowing()) {
            LogUtils.i("bl_dialog", "alertDialog>>return");
            return this;
        }

        this.context = context;
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.dialog_style);
        alertDialog = builder.create();
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_permission, null);
        alertDialog.setView(view);

        tv_dialog_title = view.findViewById(R.id.tv_dialog_title);
        tv_dialog_msg = view.findViewById(R.id.tv_dialog_msg);
        tv_dialog_left = view.findViewById(R.id.tv_dialog_left);
        tv_dialog_right = view.findViewById(R.id.tv_dialog_right);

        try {
            alertDialog.show();
        } catch (Exception e) {
        }

        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (onLinsener != null) {
                    onLinsener.onDismiss(dialog);
                }
            }
        });

        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if (onLinsener != null) {
                    onLinsener.onShow(dialog);
                }
            }
        });

        tv_dialog_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissmissDialog();
                if (onLinsener != null) {
                    onLinsener.onClick(alertDialog, 0);
                }
            }
        });

        tv_dialog_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissmissDialog();
                if (onLinsener != null) {
                    onLinsener.onClick(alertDialog, 1);
                }
            }
        });

        try {
            Window window = alertDialog.getWindow();
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.gravity = Gravity.CENTER;
            attributes.width = DpOrPxUtils.getScreenWidth(context) * 4 / 5;
            window.setAttributes(attributes);
        } catch (Exception e) {
        }

        return this;
    }

    public boolean isShowing() {
        if (alertDialog != null) {
            return alertDialog.isShowing();
        }
        return false;
    }

    public void dissmissDialog() {
        if (alertDialog != null) {
            alertDialog.dismiss();
            alertDialog = null;
        }
    }

    public PermissionDialog setUnCancle() {
        if (alertDialog != null) {
            alertDialog.setCancelable(false);
            alertDialog.setCanceledOnTouchOutside(false);
        }
        return this;
    }

    public PermissionDialog setTitle(String title) {
        if (tv_dialog_title != null) {
            tv_dialog_title.setText(title);
        }
        return this;
    }

    public PermissionDialog setTitleBg(int resourceId) {
        if (tv_dialog_title != null) {
            tv_dialog_title.setTextColor(context.getResources().getColor(R.color.white));
            tv_dialog_title.setBackground(context.getResources().getDrawable(resourceId));
        }
        return this;
    }


    public PermissionDialog setMsg(Spanned msg) {
        if (tv_dialog_msg != null) {
            tv_dialog_msg.setText(msg);
        }
        return this;
    }

    public PermissionDialog setMsg(String msg) {
        if (tv_dialog_msg != null) {
            tv_dialog_msg.setText(msg);
        }
        return this;
    }


    public PermissionDialog setLeft(String left) {
        if (tv_dialog_left != null) {
            tv_dialog_left.setText(left);
        }
        return this;
    }

    public PermissionDialog setRight(String right) {
        if (tv_dialog_right != null) {
            tv_dialog_right.setText(right);
        }
        return this;
    }

    public AlertDialog getDialog() {
        if (alertDialog != null) {
            return alertDialog;
        }
        return null;
    }

    private OnLinsener onLinsener;

    public void setOnLinsener(OnLinsener onLinsener) {
        this.onLinsener = onLinsener;
    }


    public interface OnLinsener {
        void onShow(DialogInterface dialog);

        void onDismiss(DialogInterface dialog);

        void onClick(AlertDialog dialog, int type);
    }
}